﻿//---------------------------------------------------------------------------
// <copyright file="RibbonHighlightingList.cs" company="Microsoft Corporation">
//     Copyright (C) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------

namespace Microsoft.Windows.Controls.Ribbon
{
    #region Using declarations

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    #endregion

    /// <summary>
    ///   A list of RibbonHighlightingListItems which raise an event upon selection.
    /// </summary>
    public class RibbonHighlightingList : ItemsControl
    {
        #region Public Events

        /// <summary>
        ///   Adds or removes handlers for the MostRecentFileSelected event.
        /// </summary>
        public event MostRecentFileSelectedEventHandler MostRecentFileSelected
        {
            add { AddHandler(MostRecentFileSelectedEvent, value); }
            remove { RemoveHandler(MostRecentFileSelectedEvent, value); }
        }

        /// <summary>
        ///   Raised when an item in the RibbonHighlightingList is selected.
        /// </summary>
        public static readonly RoutedEvent MostRecentFileSelectedEvent =
                    EventManager.RegisterRoutedEvent(
                            "MostRecentFileSelected",
                            RoutingStrategy.Bubble,
                            typeof(MostRecentFileSelectedEventHandler),
                            typeof(RibbonHighlightingList));

        #endregion

        #region Internal Properties

        /// <summary>
        ///   Gets the parent RibbonApplicationMenu.
        /// </summary>
        internal RibbonApplicationMenu RibbonApplicationMenu
        {
            get { return TemplatedParent as RibbonApplicationMenu; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        ///   Callback for selection of items in the RibbonHighlightingList.
        /// </summary>
        /// <param name="e">The event data.</param>
        public virtual void OnMostRecentFileSelected(MostRecentFileSelectedEventArgs e)
        {
            RaiseEvent(e);
        }

        #endregion

        #region Internal Methods

        /// <summary>
        ///   Internal callback for selection of items in the RibbonHighlightingList which
        ///   wraps parameters in a familiar type.
        /// </summary>
        /// <param name="selectedItem">The item that was selected.</param>
        internal void OnMostRecentFileSelectedInternal(object selectedItem)
        {
            // Close the application menu first
            Mouse.Capture(null);

            MostRecentFileSelectedEventArgs mostRecentFileSelectedEventArgs =
                    new MostRecentFileSelectedEventArgs(RibbonHighlightingList.MostRecentFileSelectedEvent, this, selectedItem);

            this.OnMostRecentFileSelected(mostRecentFileSelectedEventArgs);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        ///   Returns true if the item is (or is eligible to be) its own ItemContainer
        /// </summary>
        /// <param name="item">The item to test.</param>
        /// <returns>Return true if the item is (or is eligible to be) its own ItemContainer</returns>
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is RibbonHighlightingListItem;
        }

        /// <summary>
        ///   Create or identiy the element used to display the given item.
        /// </summary>
        /// <returns>The created RibbonHighlightingListItem.</returns>
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new RibbonHighlightingListItem();
        }

        #endregion
    }
}